#ifndef __GLF_TASK_TASK_IMPL_H_INCLUDED__
#define __GLF_TASK_TASK_IMPL_H_INCLUDED__

#include <glf/core/inlines.h>
#include <glf/core/syncEvent.h>
#include <glf/task/task.h>
#include <glf/task/taskManager.h>
#include <glf/task/taskBehavior.h>
#include <glf/task/taskGroups.h>

namespace glf {

namespace task_detail {

Runnable::~Runnable()
{
}
	
template <typename F>
TRunnable<F>::TRunnable(const F& func)
	: Functor(func)
{
}

template <typename F>
void TRunnable<F>::Run()
{
	Functor();
}

} // end namespace task_detail

template <typename F>
Task::Task(const F& f, bool manage)
	: m_DoneEvent(deferEvent)
	, m_DoneState(NONE)
	, m_IsManaged(manage)
	, m_Runnable(0)
{
	EphemeralAllocator< task_detail::TRunnable<F> > allocator;
	m_Runnable = new (allocator.allocate(1)) task_detail::TRunnable<F>(f);
}

template<typename Tag>
void Task::Push(bool signal)
{
	m_TaskGroup = task_detail::GrabGroup();
	TaskBehavior<Tag>().Push(this, signal);
}

bool Task::IsManaged() const
{
	return m_IsManaged;
}

} // end namespace glf

#endif // __GLF_TASK_TASK_H_INCLUDED__
